require(Rcpp)
setwd(wkdir)
output_version <- '2020-12-05' #2020-04-01

# lake chad shp
ntl.zones <- read_sf(dsn=paste0(wkdir,'/local/gis_data/003_boundaries/fishnet'),layer='fishnet_lake_chad_ntl_adm0_x_d01')
names(ntl.zones)
if ((length(unique(ntl.zones$OBJECTID))==dim(ntl.zones)[1])==FALSE) { break }

fishnet_all <- read_sf(dsn=paste0(wkdir,'/local/gis_data/003_boundaries/fishnet'),layer='fishnet_lake_chad_ntl_x_d01')
fishnet_pt <- read_sf(dsn=paste0(wkdir,'/local/gis_data/003_boundaries'),layer='fishnet_pt') %>%
  st_set_crs(.,4326)
names(fishnet_pt)[3]<-"joinid"
fishnet_ply <- st_filter(fishnet_all, ntl.zones, .predicate = st_intersects)
# add back Id's
fishnet_i <- st_join(fishnet_ply,fishnet_pt,left = TRUE)

fishnet_sp_i <- st_centroid(fishnet_i)
fishnet_sp_xy <- sf::st_coordinates(fishnet_sp_i)

xydf <- as.data.frame(st_drop_geometry(fishnet_sp_i[,c("JOINID")]))
spdf <- SpatialPointsDataFrame(coords = fishnet_sp_xy, data = xydf,
                               proj4string = CRS("+proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0"))
gridr <- raster(res=0.1,extent(fishnet_ply),crs=crs(spdf))
fishnet_i_grid <- raster::rasterize(spdf, gridr, field="JOINID")


##
## downloaded ACLED 2020-04-01
##
acled.df <- readxl::read_excel(paste0(wkdir,'/local/tab_data/Africa_1997-2020_Mar28-1.xlsx'))
conf.df <- subset(acled.df,COUNTRY %in% c('Nigeria','Cameroon','Chad','Niger'))
print('subset for 4 countries')
boko <- c('Islamic State (West Africa) and/or Boko Haram - Jamatu Ahli is-Sunnah lid-Dawatai wal-Jihad','Boko Haram - Jamatu Ahli is-Sunnah lid-Dawatai wal-Jihad')


##
## BH MEASURES ONLY HERE
##
conf.df <- as.data.frame(subset(conf.df, ACTOR1 %in% boko | ACTOR2 %in% boko))
print('subset for 4 countries bh 3960')
print('subset for 4 countries')
conf.df$EVENTS <- 1

# gen combination indicator
# number of events + w*number of fatalities? 
conf.df$combconf <- 1 + (conf.df$FATALITIES * 0.5)
# combined fatalities = fatalities +  0.5 for an event with no fatality

# Riots and protests (dummy RIOT) are instances in which "a group is involved in a public meeting against a government institution."
# CIVILIAN captures violence against civilians, defined in ACLED as instances where 
# "any armed group attacks unarmed civilians within a larger conflict. 
# In the ACLED codebook, these correspond to events of type 4 ("headquarters or base established") or 
# 5 ("nonviolent activity," which includes recruitment drives, incursions, and rallies).

##
## EVENTS BY EVENT TYPE HERE
##
unique(conf.df$EVENT_TYPE)
conf.df$ECIVILIAN = 0
conf.df$ECIVILIAN[conf.df$EVENT_TYPE == "Violence against civilians"] = 1
conf.df$EBATTLE = 0
conf.df$EBATTLE[conf.df$EVENT_TYPE == "Battles"] = 1
conf.df$ERIOT = 0
conf.df$ERIOT[conf.df$EVENT_TYPE %in% c("Protests","Riots")] = 1
summary(conf.df$ERIOT)
conf.df$EREBEL = 0
conf.df$EREBEL[conf.df$EVENT_TYPE == "Strategic developments"] = 1
# Explosions/Remote violence
conf.df$EEXPL_RMOT=0
conf.df$EEXPL_RMOT[conf.df$EVENT_TYPE == "Explosions/Remote violence"] = 1


##
## FATALITIES BY EVENT TYPE HERE
##
unique(conf.df$EVENT_TYPE)
conf.df$FCIVILIAN = 0
conf.df$FCIVILIAN[conf.df$EVENT_TYPE == "Violence against civilians"] = conf.df$FATALITIES[conf.df$EVENT_TYPE == "Violence against civilians"]
conf.df$FBATTLE = 0
conf.df$FBATTLE[conf.df$EVENT_TYPE == "Battles"] = conf.df$FATALITIES[conf.df$EVENT_TYPE == "Battles"]
conf.df$FRIOT = 0
conf.df$FRIOT[conf.df$EVENT_TYPE %in% c("Protests","Riots")] = conf.df$FATALITIES[conf.df$EVENT_TYPE %in% c("Protests","Riots")]
summary(conf.df$RIOT)
conf.df$FREBEL = 0
conf.df$FREBEL[conf.df$EVENT_TYPE == "Strategic developments"] = conf.df$FATALITIES[conf.df$EVENT_TYPE == "Strategic developments"]
# Explosions/Remote violence
conf.df$FEXPL_RMOT=0
conf.df$FEXPL_RMOT[conf.df$EVENT_TYPE == "Explosions/Remote violence"] = conf.df$FATALITIES[conf.df$EVENT_TYPE == "Explosions/Remote violence"]


##
## COMBINED MEASURE BY EVENT TYPE HERE: events + w*number of fatalities
## w = 0.5
unique(conf.df$EVENT_TYPE)
conf.df$CCIVILIAN = 0
conf.df$CCIVILIAN[conf.df$EVENT_TYPE == "Violence against civilians"] = conf.df$combconf[conf.df$EVENT_TYPE == "Violence against civilians"]
conf.df$CBATTLE = 0
conf.df$CBATTLE[conf.df$EVENT_TYPE == "Battles"] = conf.df$combconf[conf.df$EVENT_TYPE == "Battles"]
conf.df$CRIOT = 0
conf.df$CRIOT[conf.df$EVENT_TYPE %in% c("Protests","Riots")] = conf.df$combconf[conf.df$EVENT_TYPE %in% c("Protests","Riots")]
summary(conf.df$RIOT)
conf.df$CREBEL = 0
conf.df$CREBEL[conf.df$EVENT_TYPE == "Strategic developments"] = conf.df$combconf[conf.df$EVENT_TYPE == "Strategic developments"]
# Explosions/Remote violence
conf.df$CEXPL_RMOT=0
conf.df$CEXPL_RMOT[conf.df$EVENT_TYPE == "Explosions/Remote violence"] = conf.df$combconf[conf.df$EVENT_TYPE == "Explosions/Remote violence"]

##
## quick stats by year
out = conf.df %>% 
  group_by(YEAR) %>% 
  summarise(total = sum(FATALITIES))

geoCRS = 'EPSG:4326'
conf.spdf <- st_as_sf(conf.df, coords = c("LONGITUDE", "LATITUDE"), crs = geoCRS) 
conf.spdf$event <- 1
conf.spdf$EVENT_DATE = as.Date(conf.spdf$EVENT_DATE)
conf.spdf$month = as.integer(format(conf.spdf$EVENT_DATE,"%m"))

# create layer for each year and crop season #
# sowing, harvest and lean #
sowing = c(5:7)
growing = c(8:9)
sowgrowing = unique(c(sowing,growing))
harvesting = c(9:12)
land_prep = c(1:4)

##
## NEED TO DEVELOP CUMULATIVE MEASURES AT GRID LEVEL
## BASELINE is 2010s
##
summary(conf.spdf$year)

require(rSDM)
cum.spdf <- as(conf.spdf,Class="Spatial")

# Extract values based on spatial geometry
cum.spdf$joinid <- raster::extract(fishnet_i_grid, cum.spdf)
any(is.na(cum.spdf$joinid))

##
conf.df <- as.data.frame(cum.spdf)
names(conf.df)
# first collapse acled by joinid + year
print("manual identification of fields to reshape")
cum.long.df = aggregate(conf.df[, c(26,28:44)], list(conf.df$joinid,conf.df$YEAR), sum)
names(cum.long.df)[1:2]<-c("joinid","year")

##
##
##
print("base year is 2009?")
years_df <- as.data.frame(c(2009:2020))
names(years_df)<-"year"
joinid_df <- as.data.frame(unique(cum.long.df$joinid))
names(joinid_df)<-"joinid"
extended_time_data <- tidyr::expand_grid(years_df,joinid_df)

# expand to include all years above #
test <- dplyr::left_join(extended_time_data,cum.long.df,by=c('joinid','year'))
test[is.na(test)]<-0

require(data.table)
DT <- data.table(test)
DT[order(joinid,year), Cumcombconf := cumsum(combconf), by = .(joinid)]
DT[order(joinid,year), Cumevent := cumsum(EVENTS), by = .(joinid)]
DT[order(joinid,year), Cumfatal := cumsum(FATALITIES), by = .(joinid)]

##
## events by event type
##
DT[order(joinid,year), Cumecivilian := cumsum(ECIVILIAN), by = .(joinid)]
DT[order(joinid,year), Cumebattle := cumsum(EBATTLE), by = .(joinid)]
DT[order(joinid,year), Cumeriot := cumsum(ERIOT), by = .(joinid)]
DT[order(joinid,year), Cumerebel := cumsum(EREBEL), by = .(joinid)]
DT[order(joinid,year), Cumeexplrmot := cumsum(EEXPL_RMOT), by = .(joinid)]

##
## fatalities by event type
##
DT[order(joinid,year), Cumfcivilian := cumsum(FCIVILIAN), by = .(joinid)]
DT[order(joinid,year), Cumfbattle := cumsum(FBATTLE), by = .(joinid)]
DT[order(joinid,year), Cumfriot := cumsum(FRIOT), by = .(joinid)]
DT[order(joinid,year), Cumfrebel := cumsum(FREBEL), by = .(joinid)]
DT[order(joinid,year), Cumfexplrmot := cumsum(FEXPL_RMOT), by = .(joinid)]

##
## combined by event type
##
DT[order(joinid,year), Cumccivilian := cumsum(CCIVILIAN), by = .(joinid)]
DT[order(joinid,year), Cumcbattle := cumsum(CBATTLE), by = .(joinid)]
DT[order(joinid,year), Cumcriot := cumsum(CRIOT), by = .(joinid)]
DT[order(joinid,year), Cumcrebel := cumsum(CREBEL), by = .(joinid)]
DT[order(joinid,year), Cumcexplrmot := cumsum(CEXPL_RMOT), by = .(joinid)]


cum.long.df <- as.data.frame(DT) #[,c("joinid","year","Cumcombconf","Cumevent","Cumfatal")])
cum.long.df <- cum.long.df[with(cum.long.df, order(joinid, year)),]
# delete rows with 0 
cum.long.df = cum.long.df[rowSums(cum.long.df[, -(1:2)]) > 0, ]
names(cum.long.df)[3:38] <- paste0('bh',names(cum.long.df)[3:38])
write.csv(cum.long.df,paste0(wkdir,'/proc_data/bh_acled_cum_w_all_types_base2009_2020',output_version,'.csv'),row.names=FALSE)